package com.whz.governance.http.client;

import com.alibaba.cloud.sentinel.annotation.SentinelRestTemplate;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

/**
 * 启用sentinel,通用配置http client
 *
 * @author whz
 */
@Configuration
public class SentinelHttpClientConfiguration {

    /**
     * 负载均衡的http 客户端
     */
    @Bean
    @LoadBalanced
    @ConditionalOnProperty(name = "spring.cloud.sentinel.enabled", havingValue = "false", matchIfMissing = true)
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

    @Bean
    @LoadBalanced
    @SentinelRestTemplate(
            blockHandlerClass = StaticUrlCleaner.class,
            blockHandler = "staticBlock",
            fallbackClass = StaticUrlCleaner.class,
            fallback = "staticFallBack",
            urlCleaner = "clean",
            urlCleanerClass = StaticUrlCleaner.class
    )
    @ConditionalOnProperty(name = "spring.cloud.sentinel.enabled", havingValue = "true")
    public RestTemplate sentinelRestTemplate() {
        return new RestTemplate();
    }
}